package com.yang.mypc.sliingmenu;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;

public class MainActivity extends Activity {
    private FlipperView flipperView;
    //kai
    private Button open;
    private Button close;
    //手势
    private GestureDetector gestureDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        flipperView = (FlipperView) findViewById(R.id.flipper);
        //获得menu的view
        View menu = LayoutInflater.from(this).inflate(R.layout.menu, null);
        //获得主布局deview;
        View content = LayoutInflater.from(this).inflate(R.layout.content, null);
        flipperView.addView(menu);
        flipperView.addView(content);
        open = (Button) content.findViewById(R.id.open);
        close = (Button) menu.findViewById(R.id.close);
        //打开菜单
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipperView.showMenu();
            }
        });
        //关闭菜单;
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipperView.hideMenu();
            }
        });
        gestureDetector = new GestureDetector(new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // 判断是否达到最小滑动速度，取绝对值
                if (Math.abs(velocityX) > ViewConfiguration.get(
                        MainActivity.this).getScaledMinimumFlingVelocity()) {
                    if (velocityX > 0 ) {//向右滑动
                        flipperView.showMenu();
                    } else if (velocityX < 0) {//向左滑动
                        flipperView.hideMenu();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }
}

